package com.ddwl.user.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.schema.bo.user.IdentityCardQueryBo;
import com.ddwl.user.dao.mapper.IdentityCardMapper;
import com.ddwl.user.dao.model.IdentityCard;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 类IdentityCardDao.java的实现描述：DAO
 *
 * @author zach
 * @Date :2020-06-23 10:48.
 */
@Repository
public class IdentityCardDao extends TkDao<IdentityCardMapper, IdentityCard> {

    @Resource
    private IdentityCardMapper identityCardMapper;

    /**
     * 查询用户的审批数据
     *
     * @param userId
     * @return
     */
    public IdentityCard queryUserApply(String userId) {
        IdentityCard identityCard = new IdentityCard();
        identityCard.setUserId(userId);

        return selectOne(identityCard);
    }

    public IdentityCard queryUserApplyByIdCard(String idCard) {
        IdentityCard identityCard = new IdentityCard();
        identityCard.setIdCard(idCard);
        return selectOne(identityCard);
    }

    /**
     * 查询状态通过的身份证号
     *
     * @param idCard
     * @return
     */
    public IdentityCard queryUserApplyByIdCardAndSucceedStatus(String idCard) {
        IdentityCard identityCard = new IdentityCard();
        identityCard.setIdCard(idCard);
        identityCard.setStatus(BaseEnum.ApplyEnum.SUCCEED.getCode());
        return selectOne(identityCard);
    }

    public IdentityCard queryUserApplyByPhone(String phone) {
        IdentityCard identityCard = new IdentityCard();
        identityCard.setPhone(phone);
        return selectOne(identityCard);
    }

    /**
     * 查询审批通过的用户
     *
     * @return
     */
    public List<IdentityCard> queryApply() {
        IdentityCard identityCard = new IdentityCard();
        identityCard.setStatus(BaseEnum.ApplyEnum.SUCCEED.getCode());
        return select(identityCard);
    }


    public List<IdentityCard> queryApplyList(IdentityCardQueryBo queryBo) {
        Example example = getExample();
        example.and().andEqualTo("userId", queryBo.getUserId())
                .andEqualTo("regionId", queryBo.getRegionId())
                .andEqualTo("status", queryBo.getStatus())
                .andLike("idName", MySqlUtil.rightLike(queryBo.getIdName()))
                .andLike("idCard", MySqlUtil.rightLike(queryBo.getIdCard()))
                .andLike("phone", MySqlUtil.rightLike(queryBo.getPhone()))
                .andGreaterThanOrEqualTo("gmtCreated", queryBo.getStartTime())
                .andLessThan("gmtCreated", queryBo.getEndTime());
        return baseMapper.selectByExample(example);
    }

    public void deleteByUserId(String id) {
        IdentityCard model = new IdentityCard();
        model.setUserId(id);
        identityCardMapper.deleteByUserId(model);
    }

    public IdentityCard queryUserApplyByIdCardNotUserId(String idCard, String userId) {
        Example example = getExample();
        example.and().andEqualTo("idCard", idCard)
                .andEqualTo("status", BaseEnum.ApplyEnum.SUCCEED.getCode())
                .andNotEqualTo("userId", userId)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        List<IdentityCard> list = baseMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    public List<IdentityCard> getIdCardByCodeAndUserId(String code, String userId) {
        Example example = getExample();
        example.and().andEqualTo("idCard", code)
                .orEqualTo("userId", userId);
        example.and().andEqualTo("status", BaseEnum.ApplyEnum.SUCCEED.getCode());

        List<IdentityCard> list = baseMapper.selectByExample(example);
        return list;
    }

    public void changeId(Map<String, String> map) {
        identityCardMapper.changeId(map);
    }

    public List<IdentityCard> getAll() {
        return identityCardMapper.selectAll();
    }
}
